#!/usr/bin/env bash
ROOT_PATH=$(cd $(dirname $0); pwd)
ROOT_UI_PATH=${ROOT_PATH}'/tomcat'
PID_PATH=${ROOT_PATH}'/pid'
LOG_PATH=${ROOT_PATH}'/log'
LOG_OUTPUT_PATH=${LOG_PATH}'/output'
CONF_PATH=${ROOT_PATH}'/config.yml'

export JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote"
export JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.port=9001"
export JAVA_OPTS="${JAVA_OPTS} -Djava.rmi.server.hostname=x.x.x.x"
export JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
export JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"

echo '-------------------------------------------------------- application ------------------------------------------------------------'
if [[ ! -f ${PID_PATH} ]]; then
     touch ${PID_PATH}
     echo 'pid file create success'
    else
     if [[ -s ${PID_PATH} ]]; then
         echo 'stop application at pid : '$(cat ${PID_PATH})
         kill -9 $(cat ${PID_PATH})
        else
         echo 'application is not running!'
     fi
fi

if [[ ! -d ${LOG_PATH} ]]; then
    mkdir ${LOG_PATH}
fi
echo '' > ${LOG_OUTPUT_PATH}
chmod -R 755 ./*

nohup java -jar qd-1.0-SNAPSHOT-jar-with-dependencies.jar > ${LOG_OUTPUT_PATH}  2>&1 &

echo $! > ${PID_PATH}
echo 'start application at pid : '$(cat ${PID_PATH})
tail -f ${LOG_OUTPUT_PATH}
